package com.zzl.java8.czbk.stream.test1;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * description: Stream API之flatMap（扁平化操作）
 * @author 朱正磊
 * @date 2022-06-21 14:11:17
 */
public class StreamApiTest4 {

    /**
     * 测试扁平化操作：一对多
     * description: 对整形数组流进行扁平化
     * @author 朱正磊
     * @date 2022-06-21 14:24:36
     */
    @Test
    void test1() {
        // 对整形数组流进行扁平化
        int[] intArray1 = new int[]{1, 2, 3, 4};
        int[] intArray2 = new int[]{5, 6, 7, 8};
        // 生成一个整形数组流
        Stream<int[]> stream = Stream.of(intArray1, intArray2);

        Stream<Integer> intStream = stream.flatMap(x -> Arrays.stream(x).boxed());
        // Stream<Integer> intStream = stream.flatMap(x -> IntStream.of(x).boxed());
        // IntStream intStream = stream.flatMapToInt(x -> Arrays.stream(x));
        // IntStream intStream = stream.flatMapToInt(Arrays::stream);
        // IntStream intStream = stream.flatMapToInt(x -> IntStream.of(x));
        // IntStream intStream = stream.flatMapToInt(IntStream::of);
        intStream.forEach(System.out::println);
    }

    /**
     * 测试扁平化操作：一对多
     * description: 对Map中的List进行扁平化
     * @author 朱正磊
     * @date 2022-06-21 19:41:08
     */
    @Test
    void test2() {
        // 对Map中的List进行扁平化
        Map<String, List<String>> map = new HashMap<String, List<String>>(){{
            put("01", Arrays.asList("衣服", "手机", "电脑"));
            put("02", Arrays.asList("篮球鞋", "瑜伽服", "化妆品"));
            put("03", Arrays.asList("眼镜", "手表", "戒指"));
        }};

        Stream<Map<String, List<String>>> mapStream = Stream.of(map);
        Stream<List<String>> listStream = mapStream.flatMap(x -> x.values().stream());
        // Stream<String> stringStream = listStream.flatMap(x -> x.stream());
        Stream<String> stringStream = listStream.flatMap(Collection::stream);
        stringStream.forEach(System.out::println);
    }

    /**
     * description: 综合练习
     * @author 朱正磊
     * @date 2022-06-21 20:22:56
     */
    @Test
    void test3() {
        List<PbCourse> list = new ArrayList<>();
        list.add(new PbCourse(100L, "攀博课堂-Java面向对象教程", 33));
        list.add(new PbCourse(101L, "攀博课堂-Spring全家桶教程", 33));
        list.add(new PbCourse(102L, "攀博课堂-SpringCloud微服务教程", 44));
        list.add(new PbCourse(103L, "攀博课堂-在线教育分布式项目教程", 44));
        list.add(new PbCourse(104L, "攀博课堂-电子商务项目教程", 55));

        // Map<Integer, List<PbCourse>> collect = list.stream().collect(Collectors.groupingBy(x -> x.getPrice()));
        Map<Integer, List<PbCourse>> map = list.stream().collect(Collectors.groupingBy(PbCourse::getPrice));

        Stream<Map<Integer, List<PbCourse>>> mapStream = Stream.of(map);
        Stream<List<PbCourse>> listStream = mapStream.flatMap(x -> x.values().stream());
        // Stream<PbCourse> pbCourseStream = listStream.flatMap(x -> x.stream());
        Stream<PbCourse> pbCourseStream = listStream.flatMap(Collection::stream);
        pbCourseStream.forEach(System.out::println);
    }

}
